package com.ruoyi.counsellor.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.counsellor.domain.vo.CounsellorConsoleVo;
import com.ruoyi.counsellor.mapper.CounsellorStudentStatisticMapper;
import com.ruoyi.counsellor.service.ICounsellorStudentStatisticService;
import com.ruoyi.statistic.domain.StatisticStudentEmploymentFile;
import com.ruoyi.statistic.service.IStatisticStudentEmploymentFileService;
import com.ruoyi.student.service.IStudentBaseInfoService;
import com.ruoyi.student.service.IStudentRecommendInfoService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;

@Service
public class CounsellorStudentStatisticServiceImpl implements ICounsellorStudentStatisticService {

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private ICounsellorStudentStatisticService counsellorStudentStatisticService;

    @Resource
    private IStatisticStudentEmploymentFileService statisticStudentEmploymentFileService;


    @Resource
    private CounsellorStudentStatisticMapper counsellorStudentStatisticMapper;


    @Resource
    private IStudentBaseInfoService studentBaseInfoService;


    @Resource
    private IStudentRecommendInfoService studentRecommendInfoService;

    /**
     * 查询辅导员首屏数据信息
     *
     * @param counsellorCode 辅导员工号
     * @return
     */
    public CounsellorConsoleVo selectCounsellorIndexInfo(String counsellorCode) {
        CounsellorConsoleVo vo = new CounsellorConsoleVo();
        String year = sysConfigService.selectConfigByKey("current.graduation.year");
        vo.setCurrentYear(year);
        //获取最近一天日期的文件上传日期
        Date lastDate = statisticStudentEmploymentFileService.selectLastFileDate(year);//最后一次文件提交日期
        vo.setLastDate(lastDate);

        StatisticStudentEmploymentFile statisticStudentEmploymentFile = statisticStudentEmploymentFileService.selectStatisticStudentEmploymentFileByDate(year, lastDate);
        if (ObjectUtil.isNotEmpty(statisticStudentEmploymentFile)) {
            vo.setFileUploadDate(statisticStudentEmploymentFile.getCreateTime());
        }


        Integer nationwideSystemCount = counsellorStudentStatisticMapper.selectCounsellorNationwideSystemCount(year, lastDate, counsellorCode);
        vo.setCurrentNationwideSystemCount(nationwideSystemCount);

        double result = 0;
        if (ObjectUtil.isNotEmpty(nationwideSystemCount) && !nationwideSystemCount.equals(0)) {

            Integer employmentCount = counsellorStudentStatisticMapper.selectCounsellorEmploymentTypeCountByKey(year, lastDate, counsellorCode, "employment.code");//查询辅导员就业人数
            vo.setCurrentEmploymentCount(employmentCount);
            result = ((double) employmentCount / (double) nationwideSystemCount) * 100;
            vo.setCurrentEmploymentRate(String.format("%.2f", result) + "%");// 就业率

            Integer signingCount = counsellorStudentStatisticMapper.selectCounsellorEmploymentTypeCountByKey(year, lastDate, counsellorCode, "signing.code");//查询辅导员签约人数
            vo.setCurrentSigningCount(signingCount);
            result = ((double) signingCount / (double) nationwideSystemCount) * 100;
            vo.setCurrentSigningRate(String.format("%.2f", result) + "%");//签约率

            Integer currentLiberalCount = counsellorStudentStatisticMapper.selectCounsellorEmploymentTypeCountByKey(year, lastDate, counsellorCode, "liberal.code");//查询辅导员自由职业就业人数
            vo.setCurrentLiberalEmploymentCount(currentLiberalCount);//自由职业人数
            result = ((double) currentLiberalCount / (double) nationwideSystemCount) * 100;
            vo.setCurrentLiberalEmploymentRate(String.format("%.2f", result) + "%");//自由职业率

            Integer currentNimbleEmploymentCount = counsellorStudentStatisticMapper.selectCounsellorNimbleEmploymentCount(year, lastDate, counsellorCode);//查询辅导员灵活就业人数
            vo.setCurrentNimbleEmploymentCount(currentNimbleEmploymentCount);//灵活就业人数
            result = ((double) currentNimbleEmploymentCount / (double) nationwideSystemCount) * 100;
            vo.setCurrentNimbleEmploymentRate(String.format("%.2f", result) + "%");//灵活就业率

        } else {
            vo.setCurrentEmploymentCount(0);// 就业率
            vo.setCurrentEmploymentRate("0.00%");// 就业率
            vo.setCurrentSigningCount(0);// 签约人数
            vo.setCurrentSigningRate("0.00%");// 签约率
            vo.setCurrentDifficultyCount(0);//困难生人数
            vo.setCurrentDifficultyEmploymentCount(0);//困难生就业人数
            vo.setCurrentDifficultyEmploymentRate("0.00%");//困难生就业率
            vo.setCurrentLiberalEmploymentCount(0);//自由职业人数
            vo.setCurrentLiberalEmploymentRate("0.00%");//自由职业率
            vo.setCurrentNimbleEmploymentCount(0);//灵活就业人数
            vo.setCurrentNimbleEmploymentRate("0.00%");//灵活就业率
        }

        /**
         * 困难生处理
         */
        Integer difficultyCount = counsellorStudentStatisticMapper.selectCounsellorDifficultyCount(year, lastDate, counsellorCode);
        if (ObjectUtil.isNotEmpty(difficultyCount) && !difficultyCount.equals(0)) {
            vo.setCurrentDifficultyCount(difficultyCount);
            Integer difficultyEmploymentCount = counsellorStudentStatisticMapper.selectCounsellorDifficultyEmploymentCount(year, lastDate, counsellorCode, "employment.code");//困难生人数
            vo.setCurrentDifficultyEmploymentCount(difficultyEmploymentCount);//困难生就业人数
            result = ((double) difficultyEmploymentCount / (double) difficultyCount) * 100;
            vo.setCurrentDifficultyEmploymentRate(String.format("%.2f", result) + "%");//困难生就业比率
        } else {
            vo.setCurrentDifficultyCount(0);
            vo.setCurrentDifficultyEmploymentCount(0);//困难生就业人数
            vo.setCurrentDifficultyEmploymentRate("0.00%");//困难生就业比率
        }


        //查看当前辅导员名下有多少学生
        Long studentCount = studentBaseInfoService.selectStudentCountByCounsellorCode(year, counsellorCode);
        Long collectCount = studentRecommendInfoService.selectRecommendCollectCountByCounsellorCode(year, counsellorCode);
        DecimalFormat df = new DecimalFormat("#0.00");
        vo.setCollectProgress(df.format((double)collectCount/studentCount));
        Long checkCount = studentRecommendInfoService.selectRecommendCheckCountByCounsellorCode(year, counsellorCode);
        vo.setCheckProgress(df.format((double)checkCount/studentCount));

        return vo;
    }
}
